<template>
  <section class="news-main">
    <!--    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.mainTitle"
          placeholder="输入荣誉标题搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>-->

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>荣誉管理</span>
      </section>
      <section class="main-buttons-btns" />
      <el-button style="margin-left: 10px;" @click="handleCreated ">新建</el-button>
      <el-button style="margin-left: 10px;" @click="()=>{$router.back()}">返回</el-button>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        tag="honorMain"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="false"
        :source-arr="['honorManageUrl']"
      >
        <el-table-column label="操作" fixed="right" align="center" min-width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handledel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
    </section>
    <!--新建荣誉-->
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :title="type ==='c'?'新建荣誉':'编辑荣誉'"
      center
      width="1200px"
      append-to-body
      @close="closeDialog"
    >
      <el-form
        ref="userInfo"
        v-loading="submitloading"
        :model="honorInfo"
        :rules="rules"
        class="dataForm"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="完成条件" prop="finishCondition">
          <el-radio-group v-model="honorInfo.finishCondition">
            <el-radio
              v-for="v in dict.WCTJ"
              :key="v.dictValue"
              :label="v.dictValue"
            >{{ v.dictName }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务类型" prop="taskTypeValue">
          <el-input
            v-model.trim="honorInfo.taskTypeValue"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="完成值" prop="finishValue">
          <el-input
            v-model.trim="honorInfo.finishValue"
            placeholder="请输入完成值"
          >
            <template slot="append">{{ honorInfo.taskType ==='INTEGRAL_SIGN_IN'?'天':'分钟' }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="关联荣誉" prop="honorName">
          <el-input v-model="honorInfo.honorName" :disabled="true" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="chooseHonor = true">选择荣誉</el-button>
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="dialogVisible =false">取 消</el-button>
        <el-button type="primary" style="padding: 10px 30px" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" custom-class="live-dialog" :visible.sync="chooseHonor" title="选择数据" width="1200px" center>
      <choose-honor
        ref="chooseHonor"
        @select="select"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="chooseHonor =false">取 消</el-button>
        <el-button type="primary" style="padding: 10px 30px" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import chooseHonor from '../components/choosehonor'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import { getDictByCode, queryHonor, honorInsert, del } from '../api'
import permission from '@/directive/permission/index.js'
export default {
  name: 'HonorManage',
  components: { Pagination, VTable, chooseHonor },
  directives: { permission, elDragDialog },

  data() {
    return {
      id: '',
      listQuery: {
        page: 1,
        limit: 10,
        mainTitle: ''
      },
      searchshow: true,
      list: [],
      total: 0,
      listLoading: false,
      chooseHonor: false,
      time: new Date().getTime(),

      honorInfo: {
        finishValue: '',
        finishCondition: '',
        taskType: '',
        taskTypeValue: '',
        honorName: '',
        honorManageId: '',
        honourType: '',
        integralConfigId: ''
      },
      submitloading: false,
      rules: {},
      dialogVisible: false,
      dict: {
        WCTJ: [],
        INTEGRAL: []
      },
      type: 'c'
    }
  },
  created() {
    for (const key in this.dict) {
      this.getDict(key)
    }
    if (!this.$route.query.i) {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.handleCancel()
        }
      })
    } else {
      this.honorInfo.taskType = this.$route.query.t
      this.honorInfo.taskTypeValue = this.$route.query.n
      this.id = this.$route.query.i
      this.honorInfo.integralConfigId = this.$route.query.i
      this.getList()
    }
  },
  methods: {
    /**
     * 获取字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode({ dictCode: code }).then(res => {
          this.dict[code] = (res.data.data && res.data.data.length > 0) ? res.data.data[0].list : []
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    select(info) {
      this.chooseHonor = info.status
      this.honorInfo.honorName = info.arr.honorName
      this.honorInfo.honorManageId = info.arr.id
      this.honorInfo.honourType = info.arr.honorType
    },
    closeDialog() {
      this.honorInfo.finishValue = '',
      this.honorInfo.finishCondition = '',
      this.honorInfo.honorName = '',
      this.honorInfo.honorManageId = '',
      this.honorInfo.honourType = '',
      delete this.honorInfo.id
    },
    handleCreated() {
      this.dialogVisible = true
      this.type = 'c'
    },
    handleUpdate(row) {
      console.log(row)
      this.type = 'u'
      this.dialogVisible = true
      for (const key in this.honorInfo) {
        this.honorInfo[key] = row[key]
      }
      this.honorInfo.id = row.id
      console.log(this.honorInfo)
    },
    /**
     * 提交选择的资源
     */
    handleSubmit() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        honorInsert(this.type, this.honorInfo)
          .then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.dialogVisible = false
            this.getList()
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        integralConfigId: this.id
      }
      if (this.searchshow) {
        query['mainTitle'] = this.listQuery.mainTitle
      }
      return new Promise((resolve, reject) => {
        queryHonor(query).then(response => {
          if (response.data.data && response.data.code === '200') {
            this.list = response.data.data
          }
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },
    handledel(row) {
      this.$confirm('此操作将永久删除该荣誉配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          del({ id: row.id }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    filtercolumn(scope, item) {
      return scope.row[item]
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.mainTitle = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }

  }
}
</script>
<style lang="scss">

.news-main {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;
    .el-tabs__nav-wrap::after{
      display: none;
    }
    .el-tabs__header {
      margin: 0;
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
